package ae.pcfc.etks.online.web.controller.register;

/* $Id: InquireStatusController 
 * Copyright (C) 2011, Trakhees
 *  
 *  History
 *
 * Date  			Author 					Remarks
 * -------------------------------------------------------------
 * April 10, 2011 	Neha_Chauhan 	        Created
 *
 */

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.util.WebUtils;

import ae.pcfc.etks.online.common.delegate.AppMetaDataDelegate;
import ae.pcfc.etks.online.common.util.CommonUtils;
import ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.ClientInquireStatusLogin;
import ae.pcfc.etks.online.web.util.SecurityUtils;

/**
 * Controller that handles all {@link InquireStatusController}-related requests,
 * i.e.: all requests for /client/register/inquirestatus/*
 * 
 * @author Neha Chauhan
 */

@Controller
@RequestMapping(ViewPath.INQUIRESTATUS)
public class InquireStatusController extends BaseController {

	private AppMetaDataDelegate appMetaDataDelegate;

	@Resource(name = "appMetaDataDelegate")
	public void setAppMetaDataDelegate(AppMetaDataDelegate appMetaDataDelegate) {
		this.appMetaDataDelegate = appMetaDataDelegate;
	}


	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /client/register/inquirestatus
	 * 
	 * @see WEB-INF/jsp/client/register/inquirestatus/form.jsp
	 * @see WEB-INF/jsp/client/register/inquirestatus/show.jsp
	 */

	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(Model model) {

		model.addAttribute("clientInquireStatusRegister", new ClientInquireStatusLogin());

	}

	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show(HttpServletRequest request, @RequestParam(value = "refNo", required = true) String refNo, Model model) {

		AppMetaDataDetailVO appMetaDataVO = this.appMetaDataDelegate.loadAppMetaDataByClientRefNo(SecurityUtils.decryptQueryParam(refNo));
		if(appMetaDataVO != null )
		{
			ClientInquireStatusLogin clientInquireStatusLogin = new ClientInquireStatusLogin();
			// TODO: change the logic and mapper
			getMapper().map(appMetaDataVO, clientInquireStatusLogin);
/*			if (appMetaDataVO.getStrWfStatus().equals(Constants.WORK_FLOW_STATUS_IN_PROGRESS)) {
				clientInquireStatusLogin.setStrRequestStatus(messageSource.getMessage("label.user.request.in.progress", null,
						RequestContextUtils.getLocale(request)));
			} else if (appMetaDataVO.getStrWfStatus().equals(Constants.WORK_FLOW_STATUS_REJECT)) {
				clientInquireStatusLogin.setStrRequestStatus(messageSource.getMessage("label.user.request.rejected", null,
						RequestContextUtils.getLocale(request)));
			} else if (appMetaDataVO.getStrWfStatus().equals(Constants.WORK_FLOW_STATUS_APPROVED)) {
				clientInquireStatusLogin.setStrRequestStatus(messageSource.getMessage("label.user.request.approved", null,
						RequestContextUtils.getLocale(request)));
			}
 */			
			clientInquireStatusLogin.setStrReferenceNo(SecurityUtils.decryptQueryParam(refNo));
			clientInquireStatusLogin.setStrCreatedOn(CommonUtils.getDateFromString(appMetaDataVO.getApplicationDate()));
			model.addAttribute("status", clientInquireStatusLogin);
	
		}
	}

	/**
	 * Custom handler for displaying the form, i.e.:POST requests for
	 * /client/register/inquirestatus/form.jsp ,
	 * /client/register/inquirestatus/show.jsp
	 * 
	 * @see #form(ClientInquireStatusLogin)
	 * @see WEB-INF/jsp/client/register/inquirestatus/form.jsp
	 *      WEB-INF/jsp/client/register/inquirestatus/show.jsp
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String find(
			HttpServletRequest request,
			SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.CLIENT_REGISTER_INQUIRESTATUS_MODEL_KEY) ClientInquireStatusLogin clientInquireStatusRegister,
			BindingResult errors, Model model) {

		boolean isContinue = WebUtils.hasSubmitParameter(request, ConstantsAdmin.CONTINUE_BUTTON);

		if (isContinue) {
			if (errors.hasErrors()) {

				return ViewUtils.DEFAULT_VIEW;

			} else {

				Map<String, String> paramMap = new HashMap<String, String>();
				
				paramMap.put(ConstantsAdmin.REFRENCE_NO, SecurityUtils.encryptQueryParam(clientInquireStatusRegister.getStrReferenceNo()));
				
				model.addAttribute("clientInquireStatusRegister", clientInquireStatusRegister);
				return ViewUtils.getRedirect(ViewPath.INQUIRESTATUS, PageType.SHOW, paramMap);
			}

		}
		return ViewUtils.DEFAULT_VIEW;
	}

}
